/*
 * Copyright © YOLANDA. All Rights Reserved
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.tcwlkj.uuService.utils;

import android.app.ProgressDialog;
import android.content.Context;
import android.graphics.drawable.AnimationDrawable;
import android.os.Bundle;
import android.view.InflateException;
import android.view.Window;
import android.view.animation.Animation;
import android.widget.ImageView;

import com.tcwlkj.uuService.R;




public class WaitDialog extends ProgressDialog {

    private ImageView imageView;
    private AnimationDrawable animationDrawable;
    private Context context;
    private Animation hyperspaceJumpAnimation;

    public WaitDialog(Context context) {
        super(context);
        this.context=context;
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setCanceledOnTouchOutside(false);
    }

    public WaitDialog(Context context, int theme) {
        super(context, theme);
        this.context=context;
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setCanceledOnTouchOutside(false);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        try {
            setContentView(R.layout.wait_dialog);

            imageView = (ImageView) findViewById(R.id.animation_iv);
            imageView.setImageResource(R.drawable.lottery_animlist);
            hyperspaceJumpAnimation = AnimationUtils.loadAnimation(
                    context, R.anim.loading_animation);
            // 使用ImageView显示动画
            imageView.startAnimation(hyperspaceJumpAnimation);
            new Thread(new Runnable() {
                @Override
                public void run() {
                    animationDrawable = (AnimationDrawable) imageView.getDrawable();
                    animationDrawable.start();
                }
            }).start();

        } catch (OutOfMemoryError e) {
            e.printStackTrace();
        } catch (InflateException e) {
            e.printStackTrace();
        }
    }

    @Override
    protected void onStop() {
        super.onStop();
        if (animationDrawable != null) {
            animationDrawable.stop();
        }
    }
}
